package com.baba.vote.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

/**
 * @author lyd
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel
public class OperReturnDto<T> {

  @ApiModelProperty(notes = "是否成功")
  private boolean success = true;

  @ApiModelProperty(notes = "返回状态码")
  private int code = 200;

  @ApiModelProperty(notes = "异常信息")
  private String message;

  @ApiModelProperty(notes = "返回结果")
  private T results;

  @ApiModelProperty(notes = "异常调用栈")
  private String stackTrace;

  /**
   * 格式化成功结果
   */
  public static <T> OperReturnDto<T> formaterSuccess(T results) {
    OperReturnDto operReturnDto = OperReturnDto
            .builder()
            .results(results)
            .code(200)
            .success(true)
            .build();
    return operReturnDto;
  }

  public static OperReturnDto formaterSuccess() {
    OperReturnDto operReturnDto = OperReturnDto
            .builder()
            .code(200)
            .success(true)
            .build();
    return operReturnDto;
  }

  public static <T> OperReturnDto<T> formaterSuccess(int code, T result) {
    OperReturnDto operReturnDto = OperReturnDto
        .builder()
        .code(code)
        .results(result)
        .success(true)
        .build();
    return operReturnDto;
  }

  public static <T> OperReturnDto<T> formatSuccess(String msg, T results) {
    OperReturnDto operReturnDto = OperReturnDto
            .builder()
            .success(true)
            .code(200)
            .message(msg)
            .results(results)
            .build();
    return operReturnDto;
  }

  /**
   * 格式化失败结果
   */
  public static OperReturnDto formaterFail(VoteErrorCode ddCloudErrorCode, String message,
                                           String stackTraceElements) {

    String errorMessage;

    if (ddCloudErrorCode.getIndex() == VoteErrorCode.SYS_ERR.getIndex()) {

      errorMessage = message;

    } else {

      errorMessage = StringUtils.isEmpty(message) ? ddCloudErrorCode.getValueCn() : ddCloudErrorCode.getValueCn()  + message;
    }

    OperReturnDto operReturnDto = OperReturnDto
            .builder()
            .code(ddCloudErrorCode.getIndex())
            .message(errorMessage)
            .success(false)
            .stackTrace(stackTraceElements)
            .build();

    return operReturnDto;
  }

  /**
   * 格式化失败结果
   */
  public static OperReturnDto formaterFail(VoteErrorCode ddCloudErrorCode, String message) {

    String errorMessage;

    if (ddCloudErrorCode.getIndex() == VoteErrorCode.SYS_ERR.getIndex()) {

      errorMessage = message;

    } else {

      errorMessage = StringUtils.isEmpty(message) ? ddCloudErrorCode.getValueCn() : ddCloudErrorCode.getValueCn() + message;
    }

    OperReturnDto operReturnDto = OperReturnDto
            .builder()
            .code(ddCloudErrorCode.getIndex())
            .message(errorMessage)
            .success(false)
            .build();

    return operReturnDto;
  }

  /**
   * 格式化失败结果
   */
  public static OperReturnDto formaterFail(VoteErrorCode ddCloudErrorCode) {

    OperReturnDto operReturnDto = OperReturnDto
        .builder()
        .code(ddCloudErrorCode.getIndex())
        .message(ddCloudErrorCode.getValueCn())
        .success(false)
        .results(null)
        .build();

    return operReturnDto;
  }

  /**
   * 格式化失败结果
   */
  public static <T> OperReturnDto<T> formaterFail(String message, T results) {

    String errorMessage = message;

    OperReturnDto operReturnDto = OperReturnDto
        .builder()
        .code(998)
        .message(errorMessage)
        .success(false)
        .results(results)
        .build();

    return operReturnDto;
  }

  /**
   * 格式化失败结果
   */
  public static <T> OperReturnDto<T> formaterFail(int code, String message, T results) {

    String errorMessage = message;

    OperReturnDto operReturnDto = OperReturnDto
        .builder()
        .code(code)
        .message(errorMessage)
        .success(false)
        .results(results)
        .build();

    return operReturnDto;
  }

  /**
   * 格式化失败结果
   */
  public static <T> OperReturnDto<T> formaterFail(String message) {

    String errorMessage = message;

    OperReturnDto operReturnDto = OperReturnDto
        .builder()
        .code(998)
        .message(errorMessage)
        .success(false)
        .results(null)
        .build();

    return operReturnDto;
  }


}
